package com.example.zhidao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhidao.dao.BlogMapper;
import com.example.zhidao.model.Spage.ShowPage;
import com.example.zhidao.model.po.*;
import com.example.zhidao.model.responseData.ResponseData;
import com.example.zhidao.service.AdminService;
import com.example.zhidao.service.BlogService;
import com.example.zhidao.service.ClassifyService;
import com.example.zhidao.service.UserService;
import com.example.zhidao.utils.Ujwt;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("/blog")
public class BlogController {//21

//    注意这里的type中 0为不给通过  1为通过可以发表出来  2为待审核  3为发布且得到推举

    @Autowired
    BlogService blogService;

    @Resource
    BlogMapper blogMapper;

    @Autowired
    Ujwt ujwt;

    @Autowired
    UserService userService;

    @Autowired
    AdminService adminService;


//    发布文章
    @PostMapping("/add")
    public ResponseData insertBlog(@RequestHeader("token") String token, @RequestBody Blog blog){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();
        blog.setUid(uid);

        System.out.println(blog);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");//自动生成uuid
        blog.setId(uuid);
        Date date=new Date();
        blog.setCreatetime(date);
        blog.setUpdatetime(date);
        blog.setGood(0);
        blog.setCollection(0);
        blog.setView(0);
        blog.setType(2);
        blog.setComm(0);
        blog.setAudit(null);


        boolean i=blogService.save(blog);

        if(i){
            return new ResponseData(i,true,"发布成功，静待审核",200);
        }else{
            return new ResponseData(i,false,"发布失败",201);

        }
    }


    //    删除文章
//    后面需要token校验身份
    @DeleteMapping("/del")
    public ResponseData delblog(@RequestHeader("token") String token,String id){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String t=claims.getId();

        String uid=blogService.getById(id).getUid();

        if(uid.equals(t)) {

            boolean i = blogService.removeById(id);
            if (i) {
                return new ResponseData(i, true, "删除成功", 200);
            } else {
                return new ResponseData(i, false, "删除失败", 201);
            }
        }else{
            return new ResponseData(null,false,"你不是这篇文章的主人",203);
        }
    }


    //    修改文章内容
    @PutMapping("/update")
    public ResponseData updateBlog(@RequestHeader("token") String token,@RequestBody Blog blog){

//        首先根据文章的id查找是否存在对应的文章

        Blog b=blogService.getById(blog.getId());
        if(b==null){
            return new ResponseData(null,false,"该博客不存在",204);
        }else{
            //第二步，通过解出来的token获取对比blog的uid，看看是否是本人的操作
            Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
            String uid=claims.getId();

            if(blog.getUid().equals(uid)){
                UpdateWrapper<Blog> uw=new UpdateWrapper<>();
                Date date=new Date();
                uw.eq("id",blog.getId()).set("content",blog.getContent()).set("img",b.getImg()).
                        set("title",blog.getTitle()).set("updatetime",date).set("cid",blog.getCid())
                .set("type",2);
//                修改内容标题图片，以及类别，修改时间
                boolean i=blogService.update(uw);

                if(i){
                    return new ResponseData(i,true,"修改成功,静待审核",200);
                }else{
                    return new ResponseData(null,false,"修改失败",201);
                }
            }else{
                return new ResponseData(null,false,"你不是这篇博客的主人",202);
            }
        }
    }



//    查找文章(标题)
    @GetMapping("/byTitle")
    public ResponseData selBlog(String title){
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.like("title",title);
        qw.eq("type",1);
        List<Blog> blogList=blogService.list(qw);

        if(blogList==null){
            return new ResponseData(blogList,false,"未找到相关文章",201);
        }else{
            return new ResponseData(blogList,true,"成功搜索到相关的文章",200);
        }
    }




//    上传文章的封面图片
@PostMapping("image")
private ResponseData uploadBanner(MultipartFile url){
    String fileName = url.getOriginalFilename();
//    String path = "F:/202202/upload1/";
         String path = "/home/ubuntu/blog/upload/"; //阿里云
    File file = new File(path); //3. 判断路径是否存在，不存在则新建
    if (!file.exists()){
        file.mkdir();
    }
    // 3.改名,避免重名,定义新文件的名字
    // String newName = path+File.separator+System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
    String message="图片上传失败";
    String realPath = "";//图片的真实地址
    try {
        url.transferTo(new File(path+fileName));
        message= "上传成功";
        realPath= path + fileName;  // 获取图片在服务器的真实地址
    } catch (IOException e) {
        message=message+e.toString();
        return new ResponseData(-1, false, message, 200);
    }

    if (message!="图片上传失败")
        return new ResponseData(fileName, true,  message+"记录增加成功", 200);
    else
        return new ResponseData(-1, false, "请求失败", 200);
}

    //    图片预览
//    图片预览
    @GetMapping("/cat")
    public byte[] getImage(HttpServletResponse response, String img) throws Exception {
//        File file = new File("F:/202202/upload1/" + img);
//    String path = "/var/lib/tomcat8/upload/";
    File file = new File("/home/ubuntu/blog/upload/" + img);
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }


//    根据id获取文章
    @GetMapping("/getOne")
    public ResponseData getone(String id){
        Blog blog=blogService.getById(id);

        blog.setUsername(userService.getById(blog.getUid()).getUsername());
        if(blog!=null){
            return new ResponseData(blog,true,"成功获得博客信息",200);
        }else{
            return new ResponseData(null,false,"获取失败",201);
        }
    }


//    获取所有推举博客
    @GetMapping("/recommend")
    public ResponseData selallPage(@RequestParam(defaultValue = "1") Integer page ,
                                   @RequestParam(defaultValue = "10") Integer pageSize ){
        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.eq("type",3);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));


//        开始获取数据
        List<Blog> blogList=blogMapper.selRecommendfen(3,pageBegin,pageSize);
        showPage.setData(blogList);
        System.out.println("ppppppppppppppppppppppppppppppppppppppppppp");
        System.out.println(showPage.getData());

        if(blogList!=null){
            return new ResponseData(showPage,true,"成功获取推举数据",200);
        }else{
            return new ResponseData(null,false,"获取失败",201);
        }

    }


//    获取分类博客的接口
    @GetMapping("/classify")
    public ResponseData selClassify(int cid,
                                    @RequestParam(defaultValue = "1") Integer page ,
                                    @RequestParam(defaultValue = "10") Integer pageSize ){

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.ne("type",1);
        qw.ne("type",3);
        qw.eq("cid",cid);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogMapper.selClassifyfen(cid,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }


//    根据用户id获取已经审核通过的接口
    @GetMapping("/user")
    public ResponseData selByUidBlog(String uid, @RequestParam(defaultValue = "1") Integer page ,
                                     @RequestParam(defaultValue = "10") Integer pageSize ){
        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.ne("type",1);
        qw.ne("type",3);
        qw.eq("uid",uid);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogMapper.selUidfen(uid,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }


    //获取个人博客（已上线的）
    @GetMapping("/my")
    public ResponseData selMyBlog(@RequestHeader("token") String token, @RequestParam(defaultValue = "1") Integer page ,
                                     @RequestParam(defaultValue = "10") Integer pageSize ){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.ne("type",1);
        qw.ne("type",3);
        qw.eq("uid",uid);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogMapper.selUidfen(uid,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }


//    根据标题生成  这个接口适用于搜索框的，只展示前20条
    @GetMapping("/selByTit")
    public ResponseData selByTit(String title){
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.like(title!=null,"title",title);
        qw.last("limit 20");
        List<Blog> blogList=blogService.list(qw);

        if(blogList==null){
            return new ResponseData(null,false,"查询失败",201);
        }else{
            return new ResponseData(blogList,true,"查询成功",200);

        }
    }

//    这个用于标题搜索结果  并进行分页
@GetMapping("/selByTitResult")
public ResponseData selByTitResult(String title, Page p){
    QueryWrapper<Blog> qw=new QueryWrapper<>();
    qw.like(title!=null,"title",title);
    Page<Blog> blogList=blogService.page(p,qw);

    if(blogList==null){
        return new ResponseData(null,false,"查询失败",201);
    }else{
        return new ResponseData(blogList,true,"查询成功",200);

    }
}

    //获取个人博客（未上线的）
    @GetMapping("/my/await")
    public ResponseData selMyAwaitBlog(@RequestHeader("token") String token, @RequestParam(defaultValue = "1") Integer page ,
                                  @RequestParam(defaultValue = "10") Integer pageSize ){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.eq("type",2);
        qw.eq("uid",uid);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogService.list(qw));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }


    @GetMapping("/my/no")
    public ResponseData selMyNoBlog(@RequestHeader("token") String token, @RequestParam(defaultValue = "1") Integer page ,
                                       @RequestParam(defaultValue = "10") Integer pageSize ){
        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String uid=claims.getId();

        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        QueryWrapper<Blog> qw=new QueryWrapper<>();
        qw.eq("type",0);
        qw.eq("uid",uid);
        showPage.setSize(blogService.list(qw).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogService.list(qw));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }







// -------------------------------------------------------------------------------------------------------------------//

//    下面全是管理员的接口



//    根据条件获取为审核的接口
    @GetMapping("/adminGetBlog")
    public ResponseData getblog(@RequestHeader String token, Blog blog, String cname,String condition,String sort,
                                @RequestParam(defaultValue = "1") Integer page ,
                                @RequestParam(defaultValue = "10") Integer pageSize ){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }




        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        showPage.setSize(blogMapper.admingetblogtotal(blog.getTitle(),blog.getUsername(),cname).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogMapper.admingetblog(blog.getTitle(),blog.getUsername(),cname,condition,sort,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }

//获取待审核的文章
@GetMapping("/admin/awaitBlog")
public ResponseData getAwaitblog(@RequestHeader String token,Blog blog, String cname,String condition,String sort,
                                 @RequestParam(defaultValue = "1") Integer page ,
                            @RequestParam(defaultValue = "10") Integer pageSize ){


    Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
    String aid=claims.getId();
    QueryWrapper<Admin> q=new QueryWrapper();
    q.eq("id",aid);
    if(adminService.getOne(q)==null){
        return new ResponseData(null,false,"你不是管理员",300);
    }



    ShowPage showPage = new ShowPage();
    Integer pageBegin = (page-1) * pageSize;

    //当前页码
    showPage.setPageNum(page);
    //前（后）一页页码
    showPage.setPrePage(page - 1);
    showPage.setNextPage(page + 1 );
    //总数据条数
    showPage.setSize(blogMapper.adminAwaitBlogTotal(blog.getTitle(),blog.getUsername(),cname).size());
    //开始（末尾）数据是第几行
    showPage.setStartRow(pageBegin + 1);
    showPage.setEndRow(pageBegin + pageSize + 1);
    //每页数据条数
    showPage.setPageSize(pageSize);
    //总页数
    Integer div = showPage.getSize()/pageSize;
    Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
    showPage.setPages(pages);
    //是否是第（最后）一页,是否有前（后）一页
    showPage.setIsFirstPage(page == 1);
    showPage.setHasPreviousPage(page != 1);
    showPage.setIsLastPage(page.equals(pages));
    showPage.setHasNextPage(!page.equals(pages));

    showPage.setData(blogMapper.adminAwaitBlog(blog.getTitle(),blog.getUsername(),cname,condition,sort,pageBegin,pageSize));

    if(showPage.getData()!=null){
        return new ResponseData(showPage,true,"成功获取博客的数据",200);
    }else {
        return new ResponseData(null,false,"获取失败",201);
    }
}




    //获取待审核的文章
    @GetMapping("/admin/noPassBlog")
    public ResponseData getnoPassblog(@RequestHeader String token,Blog blog, String cname,String condition,String sort,
                                     @RequestParam(defaultValue = "1") Integer page ,
                                     @RequestParam(defaultValue = "10") Integer pageSize ){


        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }



        ShowPage showPage = new ShowPage();
        Integer pageBegin = (page-1) * pageSize;

        //当前页码
        showPage.setPageNum(page);
        //前（后）一页页码
        showPage.setPrePage(page - 1);
        showPage.setNextPage(page + 1 );
        //总数据条数
        showPage.setSize(blogMapper.adminNoPassBlogTotal(blog.getTitle(),blog.getUsername(),cname).size());
        //开始（末尾）数据是第几行
        showPage.setStartRow(pageBegin + 1);
        showPage.setEndRow(pageBegin + pageSize + 1);
        //每页数据条数
        showPage.setPageSize(pageSize);
        //总页数
        Integer div = showPage.getSize()/pageSize;
        Integer pages = showPage.getSize() % pageSize == 0 ? div : div + 1;
        showPage.setPages(pages);
        //是否是第（最后）一页,是否有前（后）一页
        showPage.setIsFirstPage(page == 1);
        showPage.setHasPreviousPage(page != 1);
        showPage.setIsLastPage(page.equals(pages));
        showPage.setHasNextPage(!page.equals(pages));

        showPage.setData(blogMapper.adminNoPassBlog(blog.getTitle(),blog.getUsername(),cname,condition,sort,pageBegin,pageSize));

        if(showPage.getData()!=null){
            return new ResponseData(showPage,true,"成功获取博客的数据",200);
        }else {
            return new ResponseData(null,false,"获取失败",201);
        }
    }



    //    审核文章(面向管理员编程)
    @PutMapping("/admin/audit")
    public ResponseData audit(@RequestHeader String token,@RequestBody Blog blog){


        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }



        UpdateWrapper<Blog> uw=new UpdateWrapper<>();
        Date date=new Date();
        uw.eq("id",blog.getId()).set("content",blog.getContent()).set("type",3).set("updatetime",date).set("audit",blog.getAudit());

        boolean i=blogService.update(uw);

        if(i){
            return new ResponseData(i,true,"修改成功,静待审核",200);
        }else{
            return new ResponseData(i,false,"修改失败",201);
        }
    }



    @DeleteMapping("/admin/delBlog")
    public ResponseData adminDelBlog(@RequestHeader String token,String id){

        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }


            boolean i = blogService.removeById(id);
            if (i) {
                return new ResponseData(i, true, "删除成功", 200);
            } else {
                return new ResponseData(i, false, "删除失败", 201);
            }

    }


    @PutMapping("/admin/update")
    public ResponseData adminupdate(@RequestHeader String token,String id ,int type,String audit){


        Claims claims=(Claims)ujwt.tokenToOut(token);//强制转换属性
        String aid=claims.getId();
        QueryWrapper<Admin> q=new QueryWrapper();
        q.eq("id",aid);
        if(adminService.getOne(q)==null){
            return new ResponseData(null,false,"你不是管理员",300);
        }



        UpdateWrapper<Blog> uw=new UpdateWrapper<>();

        if(type==0){
            uw.eq("id",id).set("type",type).set("audit",audit);
        }else{
            uw.eq("id",id).set("type",type).set("audit",null);
        }

        boolean i=blogService.update(uw);

        if(i){
            return new ResponseData(i,true,"修改成功",200);
        }else{
            return new ResponseData(i,false,"修改失败",201);
        }
    }




}
